//
//  InformationArticleTableView.m
//  1gene
//
//  Created by 1jiyin on 16/3/23.
//  Copyright © 2016年 1jiyin. All rights reserved.
//
#import "UILabel+VerticalAlign.h"
#import "InformationArticleTableView.h"

@implementation InformationArticleTableView
-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    if (self=[super initWithFrame:frame style:style]) {
        self.delegate=self;
        self.dataSource=self;
        self.showsVerticalScrollIndicator=NO;
        self.separatorStyle=UITableViewCellSeparatorStyleNone;
        self.tableHeaderView=[UIView new];
        self.tableFooterView=[UIView new];
        
    }
    return self;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    for (id subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    
    //图片
    UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 90-10*2, 90-10*2)];
    //imgView.image=[UIImage imageNamed:_dataArray[indexPath.row][@"imageUrl"]];
    [imgView setImageWithURL:[NSURL URLWithString:_dataArray[indexPath.row][@"img_url"]] placeholderImage:[UIImage imageNamed:@"noImage_background"]];
    [cell.contentView addSubview:imgView];
    
    //标题
    UILabel *title=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imgView.frame)+3, imgView.frame.origin.y, self.frame.size.width-(CGRectGetMaxX(imgView.frame)+3)-10, 18)];
    title.text=_dataArray[indexPath.row][@"title"];
    title.textColor=[UIColor blackColor];
    title.font=[UIFont systemFontOfSize:15];
    [cell.contentView addSubview:title];
    
    
    //内容
    UILabel *content=[[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(imgView.frame)+3, CGRectGetMaxY(title.frame)+5, self.frame.size.width-(CGRectGetMaxX(imgView.frame)+3)-10, 70-title.frame.size.height-5)];
    content.text=_dataArray[indexPath.row][@"description"];
    content.textColor=[UIColor grayColor];
    content.font=[UIFont systemFontOfSize:13.5];
    content.numberOfLines=3;
    [content alignTop];
    //    if ([content boundingRectWithSize:CGSizeMake(content.frame.size.width, 0)].height>(50/3.0)*2) {
    //
    //    }else{
    //        content.frame=CGRectMake(CGRectGetMaxX(imgView.frame)+3, CGRectGetMaxY(title.frame), self.frame.size.width-(CGRectGetMaxX(imgView.frame)+3)-10, [content boundingRectWithSize:CGSizeMake(content.frame.size.width, 0)].height);
    //    }
    
    [cell.contentView addSubview:content];
    
    //分割线
    UIView *line=[[UIView alloc]initWithFrame:CGRectMake(0, 90-1, self.frame.size.width, 1)];
    line.backgroundColor=[UIColor colorWithRed:235/255.0 green:235/255.0 blue:241/255.0 alpha:1];
    [cell.contentView addSubview:line];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 90;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *titleId=_dataArray[indexPath.row][@"id"];
    [self.myDelegate returnTitleId:titleId];
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
